package com.guli.secKill.controller;

import com.guli.common.pojo.SeckillSkuRelationEntity;
import com.guli.common.to.SecKillSessionAndRelation;
import com.guli.common.utils.R;
import com.guli.common.valid.SaveGroup;
import com.guli.secKill.service.SecKillService;
import com.guli.secKill.vo.SecKillInfoVo;
import com.guli.secKill.vo.SecKillRelationAndSpu;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.objects.annotations.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/*
 * @Author 罗俊
 * @date 2020/12/1 - 2:16 下午
 *
 */
@RestController
@Slf4j
@RequestMapping("/seckill/front")
public class SecKillController {

    @Resource
    SecKillService secKillService;

    @GetMapping("/getAvailableSecKillSpu")
    public R getAvailableSecKillSpu() {

        List<SecKillRelationAndSpu> list = secKillService.getAvailableSecKillSpu();

        return R.ok().put("data", list);
    }

    @GetMapping("/checkWhetherTheProductParticipatesInTheSpike/{spuId}")
    @ApiOperation("根据spuId查询商品是否参加秒杀")
    public List<SecKillSessionAndRelation> checkWhetherTheProductParticipatesInTheSpike(@PathVariable("spuId") String spuId) {

        return secKillService.checkWhetherTheProductParticipatesInTheSpikeAndReturnRelation(spuId);

    }

    @GetMapping("/refreshCache")
    @ApiOperation("刷新缓存（清除缓存中已上架的商品（场次信息、秒杀商品信息、库存信号量），并将数据库中最新的数据放入缓存")
    public R refreshCache(){

        secKillService.refreshCache();

        return R.ok();
    }

    @PostMapping("/executeSecKill")
    @ApiOperation("用户参与的秒杀接口")
    public R executeSecKill(@RequestBody @Validated(SaveGroup.class) SecKillInfoVo secKillInfoVo){

        log.info("此次秒杀的请求信息是：{}",secKillInfoVo);

        String orderSn = this.secKillService.executeSecKill(secKillInfoVo);

        return R.ok().put("orderSn",orderSn);
    }
}
